<template>
  <div class="login_container">
    <div class="box">
      <!-- 头像区域 -->
      <div class="avatar-box">
        <img src=".././assets/logo.png" alt="" />
      </div>
      <!-- 这是登录表单 :model 是表单数据对象  rules 是验证规则  ref 是用来获取表单里面的输入内容用来重置-->
      <el-form
        :model="formlogin"
        :rules="loginrules"
        ref="ruleForm"
        label-width="0px"
        class="login_form"
      >
        <!-- 登陆框 prop=username 是验证规则里面的 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="iconfont icon-user"
            v-model="formlogin.username"
          ></el-input>
        </el-form-item>
        <!-- 密码框 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            v-model="formlogin.password"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 提交注册框 -->
        <el-form-item class="form_button">
          <el-button type="primary" @click="loginsubmit">提交</el-button>
          <el-button @click="loginreset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 表单数据绑定
      formlogin: {
        username: 'admin',
        password: '123456'
      },
      // 表单数据验证规则
      loginrules: {
        // 用户名验证
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        // 密码验证
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // resetFields 方法对表单进行重置
    loginreset() {
      this.$refs.ruleForm.resetFields()
    },
    loginsubmit() {
      this.$refs.ruleForm.validate(async (valid) => {
        console.log(valid)
        // 这是进行预先验证,这个验证的结果是用户输入的满足输入要求
        if (!valid) return
        // 这是发送axios请求 await 是处理返回的promise数据
        const { data: res } = await this.$http.post('login', this.formlogin)
        console.log(res.data.token)
        // console.log(result)
        if (res.meta.status !== 200) return this.$message.error('登陆失败')
        this.$message.success('登陆成功')
        // 将用户登录信息存储到session
        window.sessionStorage.setItem('token', res.data.token)
        // 编程式导航，登录成功后跳转到home页面
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  //    让盒子居中显示方式一
  //     display: flex;
  //     justify-content: center;
  //    align-items: center;
}
.box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  box-sizing: border-box;
  // padding: 0 10px;
  // 让盒子居中显示方式二
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 5px;
  .avatar-box {
    width: 130px;
    height: 130px;
    border: 1px solid #ccc;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ccc;
    position: absolute;
    left: 50%;
    top: -25%;
    // transform: translate(-50%,-50%);
    transform: translateX(-50%);
    background-color: #fff;
    // img {
    // width: 100%;
    // height: 100%;
    // }
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #ccc;
    }
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  box-sizing: border-box;
  padding: 0 10px;
  .form_button {
    display: flex;
    justify-content: flex-end;
  }
}
</style>